package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author rookie001
 * @creat 2023-04-25-14:29
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    //套餐分页
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        Page<Object> objects = PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.getPageSize(setmealPageQueryDTO);
        return new PageResult(page.getPageSize(), page.getResult());
    }

    //套餐新增
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);//插入数据
        Long setmealid = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes.size() > 0 && setmealDishes != null) {
            setmealDishes.forEach(setmealDish->{
                setmealDish.setSetmealId(setmealid);
            });
          setmealDishMapper.insertDish(setmealDishes);//插入菜品
        }
    }

    //根据id查询
    @Override
    public SetmealVO getByid(Long id) {
        Setmeal setmeal = setmealMapper.getByid(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishByid(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    //修改套餐
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐表
        setmealMapper.update(setmeal);
        //删除套餐菜品表数据
        setmealDishMapper.deleteByDishId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes.size() > 0 && setmealDishes != null) {
            setmealDishes.forEach(setmealDish->{
               setmealDish.setSetmealId(setmealDTO.getId());
            });

        }
        setmealDishMapper.insertDish(setmealDishes);
    }
    //批量删除
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            //查询当前套餐是否是起售状态
            Setmeal setmeal = setmealMapper.getByid(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
//        //判断套餐是否被菜品关联
//       List<Long> dishids= setmealDishMapper.getDishIdsBySetmealIds(ids);
//        if (dishids.size()>=0&&dishids!=null){
//            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
//        }
        for (Long id : ids) {
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteByDishId(id);
//            setmealDishMapper.deleteBySetmealId(id);
        }
    }
    //套餐起售停售
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal=Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }
    //条件查询
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
       List<Setmeal> list=setmealMapper.list(setmeal);
       return list;
    }
    //根据id查询菜品选项
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
